package com.df.springmvc.system.manage.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.common.bean.DhtmlGridBean;
import com.common.bean.DhtmlGridFieldBean;
import com.common.util.DhtmlGrid.DhtmlGridAttribuate;
import com.common.util.security.EncrypMD5;
import com.df.springmvc.supper.SupperServices;
import com.df.springmvc.system.log.services.LogServices;
import com.df.springmvc.system.manage.dao.IndexDao;
import com.df.springmvc.system.manage.model.MenuTreeBean;
import com.google.gson.Gson;

/**
 * 本类只做数据运算处理
 * @author David
 */
@Service
public class IndexServices extends SupperServices{
	@Autowired
	IndexDao dao;
	
	@Autowired
	LogServices logServices;
	/**
	 * @param args
	 */
	public static void main(String[] args){
		// TODO Auto-generated method stub
	}
	public List<Object> findAllEmployees(){
		// TODO Auto-generated method stub
		try{
			Object list=dao.getUserInfo();
			System.out.println(list);
			DhtmlGridBean bean=new DhtmlGridBean();
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public Object getMenuJson() throws Exception{
		// TODO Auto-generated method stub
		String userCode = getCurrentUserBean().getUserId();
		ArrayList<HashMap<String,Object>> menuInfo=dao.queryAdminMenuInfo(userCode);
		ArrayList<MenuTreeBean> list=new ArrayList<MenuTreeBean>();
		// MenuTreeBean bean=new MenuTreeBean();
		for(HashMap<String,Object> tmp:menuInfo){
			MenuTreeBean bean=new MenuTreeBean();
			bean.setId(tmp.get("MODULE_ID"));
			bean.setName(tmp.get("MODULE_NAME"));
			bean.setIcon(tmp.get("ICON"));
			bean.setLevel(tmp.get("lvl"));
			bean.setUrl(tmp.get("URL"));
			bean.setPid(tmp.get("MODEL_PID"));
			list.add(bean);
		}
		ArrayList<MenuTreeBean> list2=getList(list,"0");
		return list2;
	}
	public ArrayList<MenuTreeBean> getList(ArrayList<MenuTreeBean> list,Object pid){
		ArrayList<MenuTreeBean> childMenu=new ArrayList<MenuTreeBean>();// 声明一个集合
		for(MenuTreeBean menuTreeBean:list){//
			if(pid != null && menuTreeBean.getPid() != null && pid.equals(menuTreeBean.getPid())){
				ArrayList<MenuTreeBean> list2=getList(list,menuTreeBean.getId());
				menuTreeBean.setList(list2);
				childMenu.add(menuTreeBean);
			}
		}
		return childMenu;
	}
	/**
	 * 个人信息维护
	 * @throws Exception 
	 */
	public Object view(HashMap<String, String> map) throws Exception {
		// TODO Auto-generated method stub
		//map.put("userId",getCurrentUserBean().getUserId());
		String loginName = getCurrentUserBean().getUserId();//获取登录名
		return dao.view(loginName);
	}
	/**
	 * 查询消息未读总条数
	 * @param map 当前登录名
	 * @return
	 * @throws Exception
	 */
	public Object getMessageList(HashMap<String, String> map) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		String loginName = getCurrentUserBean().getUserId();//获取登录名
		list = dao.getMessageList(loginName);
		return list;
	}
	/**
	 * 查询消息表格内容
	 */
	public DhtmlGridBean queryMessage(DhtmlGridBean gridBean,HashMap<String,String> param_HashMap) throws Exception{
		// TODO Auto-generated method stub
		//CurrentUserBean  userBean=CurrentUserBean.loadFromSession();
		Object[] attribute=new Object[]{};
		gridBean.cleanSessionXmlFile();
		DhtmlGridAttribuate attribuate=new DhtmlGridAttribuate();
		attribuate.addFieldBean(new DhtmlGridFieldBean("","",DhtmlGridFieldBean.TYPE_CH,"40"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("guid","<center></center>",DhtmlGridFieldBean.TYPE_ED,"0"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("messageCode","<center></center>",DhtmlGridFieldBean.TYPE_ED,"0"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("socialCid","消息内容",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		attribuate.addFieldBean(new DhtmlGridFieldBean("supplierName","消息来源",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		attribuate.addFieldBean(new DhtmlGridFieldBean("address","接收时间",DhtmlGridFieldBean.TYPE_ED,"120",DhtmlGridFieldBean.ALIGN_CENTER));
		attribuate.addFieldBean(new DhtmlGridFieldBean("receiveTime","状态",DhtmlGridFieldBean.TYPE_ED,"100",DhtmlGridFieldBean.ALIGN_CENTER));
		/**
		 * 增加对应的业务代码
		 */
		ArrayList<Object> list=null;
		gridBean.setAttribuate(attribuate);
		param_HashMap.put("userId",getCurrentUserBean().getUserId());
		list=dao.getMessageList(param_HashMap,attribute,0,2000);
		for(int i=0;i < list.size();i++){
			gridBean.addData(list.get(i));
			if((i + 1) % gridBean.getPageSize() == 0){
				gridBean.createSessionXmlFile();
			}
		}
		gridBean.createSessionXmlFile();
		return gridBean;
	}
	/**
	 * 修改消息状态
	 * @param map 
	 * @return
	 * @throws Exception
	 */
	public Object updMessage(HashMap<String, String> map) throws Exception {
		// TODO Auto-generated method stub
		HashMap<String,Object> updmap=new HashMap<String,Object>(map);
		updmap.put("updateby",getCurrentUserBean().getUserId());
		updmap.put("updatetime",new Date());
		updmap.put("isRead", "1");
		int o=(Integer) dao.updMessage(updmap);
		if(o>0){
			logServices.add("用户日志", "首页", "updMessage", "消息状态标记成功");
			return "修改成功！";
		}else{
			logServices.add("用户日志", "首页", "updMessage", "消息状态标记失败");
			throw new Exception("修改失败！");
		}
		
	}
	/**
	 * 修改密码
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	public Object updPassword(HashMap<String, String> map) throws Exception {
		// TODO Auto-generated method stub
		HashMap<String,Object> updmap=new HashMap<String,Object>(map);
		updmap.remove("oldPassword");
		updmap.put("UPDATEBY",getCurrentUserBean().getUserId());
		updmap.put("UPDATETIME",new Date());
		updmap.put("USERACCOUNT", getCurrentUserBean().getUserId());
		updmap.put("PASSWORD",EncrypMD5.encode(map.get("password")));//md5密码加密
		Object o=dao.updPassword(updmap);
		if("1".equals(o.toString())){
			return "修改成功！";
		}else{
			throw new Exception("修改失败！");
		}
	}
	@Override
	public Object upd(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		int successCount=0;
		int errorCount=0;
		for(String item:("" + paramMap.get("guid")).split(",")){
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("guid",item);
			map.put("isRead", "1");
			map.put("updateby",getCurrentUserBean().getUserId());
			map.put("updatetime",new Date());
			if("1".equals(dao.upd(map))){
				successCount++;
			}else{
				errorCount++;
			}
		}
		logServices.add("用户日志","首页","upd","消息标记成功[" + successCount + "]失败[" + errorCount + "]");
		return "消息标记成功[" + successCount + "]条，失败[" + errorCount + "]条";
	}

}
